﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using DgvFilterPopup;

namespace GRWL.BZSC.UI
{
    public partial class FindBillFrm : BaseFrm
    {

        /// <summary>
        /// 选择单据类型
        /// </summary>
        public GRWL.BZSC.Common.EnumCommon.BillType SelectBillType { get; set; }

        /// <summary>
        /// DataGridView当前选中项
        /// </summary>
        public DataRow SelectedItems { get; set; }

        /// <summary>
        /// 是否允许多选或单选
        /// <summary>
        public bool AllowMulitSelect { get; set; }

        /// <summary>
        /// 查找数据方式
        /// </summary>
        public SearchModeType SearchMode { get; set; }

        public enum SearchModeType
        {
            /// <summary>
            /// 打开单据
            /// </summary>
            open = 1,
            /// <summary>
            /// 来源单据
            /// </summary>
            source
        }

        /// <summary>
        /// 过滤器
        /// </summary>
        DgvFilterManager _DgvFm = null;

        /// <summary>
        /// 当前数据表
        /// </summary>
        DataTable _dt = null;

        public string status
        {
            set
            {
                this.cbBoxStatus.Text = value;
                this.cbBoxStatus.Enabled = false;
            }
        }

        public FindBillFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
        }

        /// <summary>
        /// Dgv字段绑定过滤器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void initDgvFilter(object sender, ColumnFilterEventArgs e)
        {
            switch (e.Column.DataPropertyName)
            {
                case "OrderCode":
                    {
                        e.ColumnFilter = new DgvComboBoxColumnFilter();
                        break;
                    }
                case "TraderName":
                    {
                        e.ColumnFilter = new DgvComboBoxColumnFilter();
                        break;
                    }
                case "Num":
                    {
                        e.ColumnFilter = new DgvDateRangeColumnFilter();
                        break;
                    }
                case "FinishedNum":
                    {
                        e.ColumnFilter = new DgvDateRangeColumnFilter();
                        break;
                    }
                case "OrderDate":
                    {
                        e.ColumnFilter = new DgvDateColumnFilter();
                        break;
                    }
                case "DeliveryDate":
                    {
                        e.ColumnFilter = new DgvDateColumnFilter();
                        break;
                    }
                case "QCDate":
                    {
                        e.ColumnFilter = new DgvDateColumnFilter();
                        break;
                    }
                case "ReqDate":
                    {
                        e.ColumnFilter = new DgvDateColumnFilter();
                        break;
                    }
                case "Status":
                    {
                        e.ColumnFilter = new DgvComboBoxColumnFilter();
                        break;
                    }
                case "operator":
                    {
                        e.ColumnFilter = new DgvComboBoxColumnFilter();
                        break;
                    }

                default:
                    {
                        e.ColumnFilter = new DgvTextBoxColumnFilter();
                        break;
                    }
            }
            e.Handled = true;
        }

        /// <summary>
        /// 初始化显示
        /// </summary>
        private void initDgvColumns()
        {
            this.dataGridView1.AutoGenerateColumns = false;

            Common.BindEnumToComboBox(this.cbBoxStatus, typeof(BZSC.Common.EnumCommon.VerifyStatus));
            switch (this.SelectBillType)
            {
                case GRWL.BZSC.Common.EnumCommon.BillType.CG:
                    {
                        this.lblTrader.Visible = true;
                        this.cbBoxTrader.Visible = true;
                        bindTrader();

                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "供应商", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderDate", "采购日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DeliveryDate", "交货日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "采购物资", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Num", "数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "FinishedNum", "完成数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);

                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.DH:
                    {
                        this.lblTrader.Visible = true;
                        this.cbBoxTrader.Visible = true;
                        bindTrader();
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "客户", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderDate", "订货日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DeliveryDate", "交货日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialId", "订货物资编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "订货物资", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Num", "数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "FinishNum", "完成数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCD:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "订单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderDate", "订单日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "生产单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "prdnDate", "日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "批准", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "materialcode", "产品代码", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "产品名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Category", "类别", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "PrdnType", "生产类型", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DepartmentName", "生产部门", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "PlanOutput", "计划产量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "FinishNum", "完成数量", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.CGZJ:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "质检单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "QCDate", "质检日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialCode", "物资代码", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "物资名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "CheckNum", "检查数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DeliveryNum", "交货数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OperatorName", "检验人", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCZJ:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "质检单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "QCDate", "质检日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialCode", "物资代码", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "物资名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "CheckNum", "检查数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DeliveryNum", "交货数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OperatorName", "检验人", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.ZS:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "供应商", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "物资", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "ShippingNum", "数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialSpecs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Unit", "单位", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "CategoryName", "分类", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "orderCode", "订货单", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "qcCode", "质检单", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.QG:
                    {
                        this.lblTrader.Visible = true;
                        this.cbBoxTrader.Visible = true;

                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "ReqDate", "请购日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Materialname", "物料名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "CategoryName", "分类", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Num", "数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "供应商", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.CGSH:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "供应商", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "物资", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "ShippingNum", "数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialSpecs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Unit", "单位", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "CategoryName", "分类", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "订货单", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "prevCode", "质检单", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCSH:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "供应商", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "物资", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "ShippingNum", "数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialSpecs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Unit", "单位", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "CategoryName", "分类", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "订货单", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "prevCode", "质检单", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.LL:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "RqrtCode", "领料单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "recvDate", "领料日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "materialcode", "物料代码", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Materialname", "物料名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Num", "数量", true, true, null);
                        break;

                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.BJ:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "quoteDate", "报价日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Materialname", "物料名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "供应商", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Unit", "单位", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.XQDD:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderType", "类型", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "TraderName", "客户", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderDate", "订货日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DeliveryDate", "预交日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialCode", "产品代号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "产品名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Category", "材料分类", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Num", "订货数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "FinishedNum", "发货数量", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DetailMemo", "备注", true, true, null);

                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCXQ:
                    {
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Id", "编号", false, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Code", "计划编号", true, true, 120);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OrderCode", "订单号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "RqrtDate", "计划日期", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "OperatorName", "制表人", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialCode", "订单产品代号", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "MaterialName", "产品名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Specs", "规格", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "DepartmentName", "部门名称", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewCheckBoxColumn(), "IsRelease", "发放完成", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "Status", "审批", true, true, null);
                        Common.AddDgvColumn(this.dataGridView1, new DataGridViewTextBoxColumn(), "AssessorName", "审批人", true, true, null);
                        break;
                    }
            }
        }

        /// <summary>
        /// 加载往来单位数据
        /// </summary>
        private void bindTrader()
        {
            bool? isSupplier = null;
            bool? isClient = null;
            if (this.SelectBillType == GRWL.BZSC.Common.EnumCommon.BillType.CG || this.SelectBillType == GRWL.BZSC.Common.EnumCommon.BillType.QG)
            {
                this.lblTrader.Text = "供应商";
                isSupplier = true;
            }
            else
            {
                this.lblTrader.Text = "客户";
                isClient = true;
            }
            this.cbBoxTrader.Items.Clear();
            TB_Trader[] traders = this.BaseDataProxy.FindTraders(null, isSupplier, isClient, null, null, null);
            if (traders != null)
            {
                foreach (TB_Trader trader in traders)
                    this.cbBoxTrader.Items.Add(trader);
                this.cbBoxTrader.ValueMember = "id";
                this.cbBoxTrader.DisplayMember = "name";
            }
        }

        /// <summary>
        /// 加载物料分类数据
        /// </summary>
        private void bindCategory()
        {
            this.cbBoxCategory.Items.Clear();
            TB_Options[] options = this.BaseDataProxy.FindOptions(GRWL.BZSC.Common.EnumCommon.OptionType.物料分类.ToString(), null, null);
            if (options != null)
            {
                this.cbBoxCategory.ValueMember = "id";
                this.cbBoxCategory.DisplayMember = "name";
                foreach (TB_Options option in options)
                    this.cbBoxCategory.Items.Add(option);

            }
        }

        private void FindBillFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            bindCategory();
            initDgvColumns();
            this._DgvFm = new DgvFilterManager();
            this._DgvFm.ColumnFilterAdding += new ColumnFilterEventHandler(initDgvFilter);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this._DgvFm.DataGridView != null)
                this._DgvFm.ActivateAllFilters(false);

            int? category = null;
            if (this.cbBoxCategory.SelectedValue != null)
                category = Convert.ToInt32(this.cbBoxCategory.SelectedValue);
            int? trader = null;
            if (this.cbBoxTrader.SelectedValue != null)
                trader = Convert.ToInt32(this.cbBoxTrader.SelectedValue);
            switch (SelectBillType)
            {
                case GRWL.BZSC.Common.EnumCommon.BillType.CG:
                    {
                        QOrder[] xQOrder;
                        if (this.SearchMode == SearchModeType.source)
                            xQOrder = BaseDataProxy.FindOrders(GRWL.BZSC.Common.EnumCommon.BillType.CG.ToString(), this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text, null, null, true);
                        else
                            xQOrder = BaseDataProxy.FindOrders(GRWL.BZSC.Common.EnumCommon.BillType.CG.ToString(), this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text, null, null, null);
                        //this.SelectedItems = xQOrder;
                        this._dt = xQOrder.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.DH:
                    {
                        QOrder[] xQOrder = BaseDataProxy.FindOrders(GRWL.BZSC.Common.EnumCommon.BillType.DH.ToString(), this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text, null, null,null);
                        //this.SelectedItems = xQOrder;
                        this._dt = xQOrder.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCD:
                    {
                        QProduction[] xQProduction = null;
                        if (this.SearchMode == SearchModeType.source)
                            xQProduction = BaseDataProxy.FindProductions(this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text, true);
                        else
                            xQProduction = BaseDataProxy.FindProductions(this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text, null);
                        //this.SelectedItems = xQProduction;
                        this._dt = xQProduction.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.CGZJ://入仓时选单（采购质检）
                    {
                        QQC[] qqc;
                        if (this.SearchMode == SearchModeType.open)
                            qqc = BaseDataProxy.FindQCs("", this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value);
                        else
                            qqc = BaseDataProxy.FindQCs(GRWL.BZSC.Common.EnumCommon.BillType.CGZJ.ToString(), this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value);
                        //this.SelectedItems = qqc;
                        this._dt = qqc.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCZJ://入仓时选单（生产质检）
                    {
                        QQC[] qqc = BaseDataProxy.FindQCs(GRWL.BZSC.Common.EnumCommon.BillType.SCZJ.ToString(), this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value);
                        //this.SelectedItems = qqc;
                        this._dt = qqc.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.ZS:
                    {
                        TB_QCResult[] tbTemp = BaseDataProxy.FindQCResults();
                        //this.SelectedItems = tbTemp;
                        this._dt = tbTemp.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.QG:
                    {
                        QRequisition[] xQRequsition = BaseDataProxy.FindRequisitions(this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text);
                        //this.SelectedItems = xQRequsition;
                        this._dt = xQRequsition.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.CGSH://采购收货
                    {
                        QShipping[] xQShipping;
                        if (this.SearchMode == SearchModeType.open)
                            xQShipping = BaseDataProxy.FindShippings("", this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, trader, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text);//打开时，打开所有单，包括未批
                        else
                            xQShipping = BaseDataProxy.FindShippings(GRWL.BZSC.Common.EnumCommon.BillType.CGSH.ToString(), this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, trader, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text);
                        //this.SelectedItems = xQShipping;
                        this._dt = xQShipping.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCSH://生产入库
                    {
                        QShipping[] xQShipping = BaseDataProxy.FindShippings(GRWL.BZSC.Common.EnumCommon.BillType.SCSH.ToString(), this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, trader, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text);
                        //this.SelectedItems = xQShipping;
                        this._dt = xQShipping.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.LL:
                    {
                        QRecvMaterial[] QRecvMaterial = BaseDataProxy.FindRecvMaterials(this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text);
                        //this.SelectedItems = QRecvMaterial;
                        this._dt = QRecvMaterial.ToDataTable();
                        break;

                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.BJ:
                    {
                        QQuotation[] QQuotation = BaseDataProxy.FindQuotations(this.txtBoxCode.Text, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value, this.edtEDate.Value.Date, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text);
                        //this.SelectedItems = QQuotation;
                        this._dt = QQuotation.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.XQDD:
                    {
                        QOrder[] xQOrder = BaseDataProxy.FindOrders(GRWL.BZSC.Common.EnumCommon.BillType.DH.ToString(), BillPrefix, this.txtBoxOperator.Text, this.cbBoxStatus.Text, this.edtSDate.Value.Date, this.edtEDate.Value, this.txtBoxMaterialCode.Text, category, this.txtBoxMaterialName.Text, false, true,null);
                        //this.SelectedItems = xQOrder;
                        this._dt = xQOrder.ToDataTable();
                        break;
                    }
                case GRWL.BZSC.Common.EnumCommon.BillType.SCXQ:
                    {
                        QRequirement[] qRs = this.BaseDataProxy.FindRequirements(BillPrefix, this.txtBoxOperator.Text, this.cbBoxStatus.Text, null, this.edtSDate.Value.Date, this.edtEDate.Value, null);
                        //this.SelectedItems = qRs;
                        this._dt = qRs.ToDataTable();
                        break;
                    }
            }

            this.dataGridView1.DataSource = this._dt;
            if (this._dt != null && this._dt.Rows.Count > 0)
                this._DgvFm.DataGridView = this.dataGridView1;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows != null && this.dataGridView1.SelectedRows.Count > 0)
            {
                DataRow[] drs = this._dt.Select("id=" + this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());

                if (drs != null && drs.Length > 0)
                {
                    this.SelectedItems = drs[0];
                    //switch (SelectBillType)
                    //{
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.CG:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QOrder>(drs[0]);
                    //            //this.SelectedItems = this.SelectedItems.Cast<QOrder>().Where(p => p.Id == id).ToArray();
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.DH:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QOrder>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.SCD:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QProduction>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.ZJ:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QQC>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.ZS:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<TB_QCResult>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.QG:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QRequisition>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.SH:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QShipping>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.LL:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QRecvMaterial>(drs[0]);
                    //            break;

                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.BJ:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QQuotation>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.XQDD:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QOrder>(drs[0]);
                    //            break;
                    //        }
                    //    case GRWL.BZSC.Common.EnumCommon.BillType.SCXQ:
                    //        {
                    //            this.SelectedItems[0] = OTConverter.ConvertToObject<QRequirement>(drs[0]);
                    //            break;
                    //        }
                    //}

                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("选择为空，请选择记录后再确定！");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(this.dataGridView1);
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Common.ShowDataGridViewRowNo(this.dataGridView1);
        }
    }
}
